#include <QApplication>
#include "ElaApplication.h"
#include "ElaTheme.h"
#include "ElaWidget.h"
#include "ElaToggleSwitch.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QDebug>

class ElaToggleSwitchTest : public ElaWidget
{
    Q_OBJECT

  public:
    ElaToggleSwitchTest(QWidget *parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("ElaToggleSwitch 功能测试");
        resize(600, 500);
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        setupUI();
        setupConnections();
    }

  private:
    void setupUI()
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);

        // 1. 基础开关展示
        mainLayout->addWidget(createBasicToggleGroup());

        // 2. 状态演示
        mainLayout->addWidget(createStateDemoGroup());

        // 3. 应用场景演示
        mainLayout->addWidget(createApplicationGroup());

        // 4. 动态操作演示
        mainLayout->addWidget(createDynamicOperationGroup());

        // 5. 开关组联动
        mainLayout->addWidget(createToggleGroupDemo());

        mainLayout->addStretch();
    }

    // 创建基础开关组
    QGroupBox *createBasicToggleGroup()
    {
        QGroupBox *group = new QGroupBox("基础开关功能", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示基本的开关功能和状态变化", this);
        layout->addWidget(infoLabel);

        // 默认状态开关
        QHBoxLayout *defaultLayout = new QHBoxLayout();
        m_defaultToggle = new ElaToggleSwitch(this);
        QLabel *defaultLabel = new QLabel("默认关闭状态", this);
        defaultLayout->addWidget(defaultLabel);
        defaultLayout->addWidget(m_defaultToggle);
        defaultLayout->addStretch();

        // 默认开启状态
        QHBoxLayout *enabledLayout = new QHBoxLayout();
        m_enabledToggle = new ElaToggleSwitch(this);
        m_enabledToggle->setIsToggled(true);
        QLabel *enabledLabel = new QLabel("默认开启状态", this);
        enabledLayout->addWidget(enabledLabel);
        enabledLayout->addWidget(m_enabledToggle);
        enabledLayout->addStretch();

        layout->addLayout(defaultLayout);
        layout->addLayout(enabledLayout);

        return group;
    }

    // 创建状态演示组
    QGroupBox *createStateDemoGroup()
    {
        QGroupBox *group = new QGroupBox("状态演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示开关在不同状态下的表现", this);
        layout->addWidget(infoLabel);

        // 正常状态
        QHBoxLayout *normalLayout = new QHBoxLayout();
        m_normalToggle = new ElaToggleSwitch(this);
        m_normalToggle->setIsToggled(true);
        QLabel *normalLabel = new QLabel("正常状态（可操作）", this);
        normalLayout->addWidget(normalLabel);
        normalLayout->addWidget(m_normalToggle);
        normalLayout->addStretch();

        // 禁用状态 - 关闭
        QHBoxLayout *disabledOffLayout = new QHBoxLayout();
        m_disabledOffToggle = new ElaToggleSwitch(this);
        m_disabledOffToggle->setEnabled(false);
        QLabel *disabledOffLabel = new QLabel("禁用状态 - 关闭", this);
        disabledOffLayout->addWidget(disabledOffLabel);
        disabledOffLayout->addWidget(m_disabledOffToggle);
        disabledOffLayout->addStretch();

        // 禁用状态 - 开启
        QHBoxLayout *disabledOnLayout = new QHBoxLayout();
        m_disabledOnToggle = new ElaToggleSwitch(this);
        m_disabledOnToggle->setIsToggled(true);
        m_disabledOnToggle->setEnabled(false);
        QLabel *disabledOnLabel = new QLabel("禁用状态 - 开启", this);
        disabledOnLayout->addWidget(disabledOnLabel);
        disabledOnLayout->addWidget(m_disabledOnToggle);
        disabledOnLayout->addStretch();

        layout->addLayout(normalLayout);
        layout->addLayout(disabledOffLayout);
        layout->addLayout(disabledOnLayout);

        return group;
    }

    // 创建应用场景演示组
    QGroupBox *createApplicationGroup()
    {
        QGroupBox *group = new QGroupBox("应用场景演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示开关在实际应用场景中的使用", this);
        layout->addWidget(infoLabel);

        // 系统设置组
        QGroupBox *settingsGroup = new QGroupBox("系统设置", this);
        QVBoxLayout *settingsLayout = new QVBoxLayout(settingsGroup);

        // 自动更新
        QHBoxLayout *autoUpdateLayout = new QHBoxLayout();
        m_autoUpdateToggle = new ElaToggleSwitch(this);
        m_autoUpdateToggle->setIsToggled(true);
        QLabel *autoUpdateLabel = new QLabel("自动检查更新", this);
        autoUpdateLayout->addWidget(autoUpdateLabel);
        autoUpdateLayout->addWidget(m_autoUpdateToggle);
        autoUpdateLayout->addStretch();

        // 消息通知
        QHBoxLayout *notificationLayout = new QHBoxLayout();
        m_notificationToggle = new ElaToggleSwitch(this);
        QLabel *notificationLabel = new QLabel("启用消息通知", this);
        notificationLayout->addWidget(notificationLabel);
        notificationLayout->addWidget(m_notificationToggle);
        notificationLayout->addStretch();

        // 深色模式
        QHBoxLayout *darkModeLayout = new QHBoxLayout();
        m_darkModeToggle = new ElaToggleSwitch(this);
        QLabel *darkModeLabel = new QLabel("深色模式", this);
        darkModeLayout->addWidget(darkModeLabel);
        darkModeLayout->addWidget(m_darkModeToggle);
        darkModeLayout->addStretch();

        settingsLayout->addLayout(autoUpdateLayout);
        settingsLayout->addLayout(notificationLayout);
        settingsLayout->addLayout(darkModeLayout);

        // 隐私设置组
        QGroupBox *privacyGroup = new QGroupBox("隐私设置", this);
        QVBoxLayout *privacyLayout = new QVBoxLayout(privacyGroup);

        // 数据收集
        QHBoxLayout *dataCollectionLayout = new QHBoxLayout();
        m_dataCollectionToggle = new ElaToggleSwitch(this);
        QLabel *dataCollectionLabel = new QLabel("允许数据收集", this);
        dataCollectionLayout->addWidget(dataCollectionLabel);
        dataCollectionLayout->addWidget(m_dataCollectionToggle);
        dataCollectionLayout->addStretch();

        // 位置服务
        QHBoxLayout *locationLayout = new QHBoxLayout();
        m_locationToggle = new ElaToggleSwitch(this);
        m_locationToggle->setIsToggled(true);
        QLabel *locationLabel = new QLabel("位置服务", this);
        locationLayout->addWidget(locationLabel);
        locationLayout->addWidget(m_locationToggle);
        locationLayout->addStretch();

        privacyLayout->addLayout(dataCollectionLayout);
        privacyLayout->addLayout(locationLayout);

        layout->addWidget(settingsGroup);
        layout->addWidget(privacyGroup);

        return group;
    }

    // 创建动态操作演示组
    QGroupBox *createDynamicOperationGroup()
    {
        QGroupBox *group = new QGroupBox("动态操作演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("演示开关的动态操作和控制", this);
        layout->addWidget(infoLabel);

        // 动态启用/禁用
        QHBoxLayout *enableDisableLayout = new QHBoxLayout();
        m_dynamicToggle = new ElaToggleSwitch(this);
        m_dynamicToggle->setEnabled(false);
        m_dynamicToggle->setIsToggled(true);
        QLabel *dynamicLabel = new QLabel("动态控制开关", this);
        QPushButton *toggleEnableBtn = new QPushButton("切换启用状态", this);

        enableDisableLayout->addWidget(dynamicLabel);
        enableDisableLayout->addWidget(m_dynamicToggle);
        enableDisableLayout->addWidget(toggleEnableBtn);
        enableDisableLayout->addStretch();

        // 状态控制按钮
        QHBoxLayout *controlLayout = new QHBoxLayout();
        QLabel *controlLabel = new QLabel("状态控制:", this);
        QPushButton *turnOnBtn = new QPushButton("开启", this);
        QPushButton *turnOffBtn = new QPushButton("关闭", this);
        QPushButton *toggleBtn = new QPushButton("切换", this);

        controlLayout->addWidget(controlLabel);
        controlLayout->addWidget(turnOnBtn);
        controlLayout->addWidget(turnOffBtn);
        controlLayout->addWidget(toggleBtn);
        controlLayout->addStretch();

        // 状态显示
        QHBoxLayout *statusLayout = new QHBoxLayout();
        m_statusDisplay = new QLabel("当前状态: 开启", this);

        statusLayout->addWidget(m_statusDisplay);
        statusLayout->addStretch();

        layout->addLayout(enableDisableLayout);
        layout->addLayout(controlLayout);
        layout->addLayout(statusLayout);

        m_operationButtons << toggleEnableBtn << turnOnBtn << turnOffBtn << toggleBtn;

        return group;
    }

    // 创建开关组联动演示
    QGroupBox *createToggleGroupDemo()
    {
        QGroupBox *group = new QGroupBox("开关组联动", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("演示多个开关之间的联动关系", this);
        layout->addWidget(infoLabel);

        // 主开关控制
        QHBoxLayout *masterLayout = new QHBoxLayout();
        m_masterToggle = new ElaToggleSwitch(this);
        QLabel *masterLabel = new QLabel("主开关（控制所有子开关）", this);
        masterLayout->addWidget(masterLabel);
        masterLayout->addWidget(m_masterToggle);
        masterLayout->addStretch();

        // 子开关组
        QHBoxLayout *slaveLayout = new QHBoxLayout();
        m_slaveToggle1 = new ElaToggleSwitch(this);
        m_slaveToggle2 = new ElaToggleSwitch(this);
        m_slaveToggle3 = new ElaToggleSwitch(this);
        QLabel *slaveLabel = new QLabel("子开关:", this);

        slaveLayout->addWidget(slaveLabel);
        slaveLayout->addWidget(m_slaveToggle1);
        slaveLayout->addWidget(m_slaveToggle2);
        slaveLayout->addWidget(m_slaveToggle3);
        slaveLayout->addStretch();

        layout->addLayout(masterLayout);
        layout->addLayout(slaveLayout);

        return group;
    }

    void setupConnections()
    {
        // 基础开关信号连接
        connect(m_defaultToggle, &ElaToggleSwitch::toggled, this, [this](bool checked) { qDebug() << "默认开关状态:" << (checked ? "开启" : "关闭"); });

        connect(m_enabledToggle, &ElaToggleSwitch::toggled, this, [this](bool checked) { qDebug() << "启用开关状态:" << (checked ? "开启" : "关闭"); });

        // 系统设置开关信号
        connect(m_autoUpdateToggle, &ElaToggleSwitch::toggled, this, [this](bool checked) { qDebug() << "自动更新:" << (checked ? "启用" : "禁用"); });

        connect(m_notificationToggle, &ElaToggleSwitch::toggled, this, [this](bool checked) { qDebug() << "消息通知:" << (checked ? "启用" : "禁用"); });

        connect(m_darkModeToggle, &ElaToggleSwitch::toggled, this, [this](bool checked) {
            qDebug() << "深色模式:" << (checked ? "启用" : "禁用");
            // 这里可以添加主题切换逻辑
        });

        // 隐私设置开关信号
        connect(m_dataCollectionToggle, &ElaToggleSwitch::toggled, this, [this](bool checked) { qDebug() << "数据收集:" << (checked ? "允许" : "禁止"); });

        connect(m_locationToggle, &ElaToggleSwitch::toggled, this, [this](bool checked) { qDebug() << "位置服务:" << (checked ? "启用" : "禁用"); });

        // 动态操作按钮连接
        // 切换启用状态
        connect(m_operationButtons[0], &QPushButton::clicked, this, [this]() {
            bool currentState = m_dynamicToggle->isEnabled();
            m_dynamicToggle->setEnabled(!currentState);
            QString stateText = !currentState ? "已启用" : "已禁用";
            qDebug() << "动态开关" << stateText;
        });

        // 开启
        connect(m_operationButtons[1], &QPushButton::clicked, this, [this]() {
            m_dynamicToggle->setIsToggled(true);
            qDebug() << "动态开关已开启";
        });

        // 关闭
        connect(m_operationButtons[2], &QPushButton::clicked, this, [this]() {
            m_dynamicToggle->setIsToggled(false);
            qDebug() << "动态开关已关闭";
        });

        // 切换
        connect(m_operationButtons[3], &QPushButton::clicked, this, [this]() {
            bool currentState = m_dynamicToggle->getIsToggled();
            m_dynamicToggle->setIsToggled(!currentState);
            qDebug() << "动态开关已切换至:" << (!currentState ? "开启" : "关闭");
        });

        // 动态开关状态变化
        connect(m_dynamicToggle, &ElaToggleSwitch::toggled, this, [this](bool checked) {
            QString statusText = QString("当前状态: %1").arg(checked ? "开启" : "关闭");
            m_statusDisplay->setText(statusText);
            qDebug() << "动态开关状态更新:" << statusText;
        });

        // 主从开关联动
        connect(m_masterToggle, &ElaToggleSwitch::toggled, this, [this](bool checked) {
            m_slaveToggle1->setIsToggled(checked);
            m_slaveToggle2->setIsToggled(checked);
            m_slaveToggle3->setIsToggled(checked);
            m_slaveToggle1->setEnabled(checked);
            m_slaveToggle2->setEnabled(checked);
            m_slaveToggle3->setEnabled(checked);
            qDebug() << "主开关" << (checked ? "开启" : "关闭") << "所有子开关";
        });

        // 子开关状态变化
        connect(m_slaveToggle1, &ElaToggleSwitch::toggled, this, [this](bool checked) { qDebug() << "子开关1:" << (checked ? "开启" : "关闭"); });

        connect(m_slaveToggle2, &ElaToggleSwitch::toggled, this, [this](bool checked) { qDebug() << "子开关2:" << (checked ? "开启" : "关闭"); });

        connect(m_slaveToggle3, &ElaToggleSwitch::toggled, this, [this](bool checked) { qDebug() << "子开关3:" << (checked ? "开启" : "关闭"); });

        //connect(m_disabledOffToggle, &ElaToggleSwitch::disabledClicked, this, []() { qDebug() << "禁用状态的开关被点击了！"; });
        //connect(m_disabledOnToggle, &ElaToggleSwitch::disabledClicked, this, []() { qDebug() << "禁用状态的开关被点击了！"; });
    }

  private:
    // 基础开关
    ElaToggleSwitch *m_defaultToggle;
    ElaToggleSwitch *m_enabledToggle;

    // 状态演示
    ElaToggleSwitch *m_normalToggle;
    ElaToggleSwitch *m_disabledOffToggle;
    ElaToggleSwitch *m_disabledOnToggle;

    // 应用场景
    ElaToggleSwitch *m_autoUpdateToggle;
    ElaToggleSwitch *m_notificationToggle;
    ElaToggleSwitch *m_darkModeToggle;
    ElaToggleSwitch *m_dataCollectionToggle;
    ElaToggleSwitch *m_locationToggle;

    // 动态操作
    ElaToggleSwitch *m_dynamicToggle;
    QList<QPushButton *> m_operationButtons;
    QLabel *m_statusDisplay;

    // 开关组联动
    ElaToggleSwitch *m_masterToggle;
    ElaToggleSwitch *m_slaveToggle1;
    ElaToggleSwitch *m_slaveToggle2;
    ElaToggleSwitch *m_slaveToggle3;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    ElaToggleSwitchTest test;
    test.moveToCenter();
    test.show();

    return app.exec();
}

#include "ElaToggleSwitchTest.moc"